package com.soft.servlet;

import com.soft.dao.InformationDAO;
import com.soft.entity.Information;
import com.soft.util.PageBean;

import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet({"/InformationServlet"})
public class InformationServlet
        extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String uri;

    public void destroy() {
        super.destroy();
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");


        String method = request.getParameter("method");


        if ("add".equals(method)) {

            add(request, response);

        } else if ("list".equals(method)) {

            list(request, response);

        } else if ("search".equals(method)) {

            search(request, response);

        } else if ("delete".equals(method)) {

            delete(request, response);

        } else if ("Jumpup".equals(method)) {

            Jumpup(request, response);

        } else if ("update".equals(method)) {

            update(request, response);
        }
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String informationTitle = request.getParameter("title");
            String informationAuthor = request.getParameter("author");
            String informationContent = request.getParameter("content");
            String informationDate = request.getParameter("date");

            Information information = new Information();
            information.setTitle(informationTitle);
            information.setAuthor(informationAuthor);
            information.setContent(informationContent);
            information.setDate(informationDate);


            InformationDAO dao = new InformationDAO();
            dao.add(information);

            this.uri = "/InformationServlet?method=list";
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.getRequestDispatcher(this.uri)
                .forward((ServletRequest) request, (ServletResponse) response);
    }


    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String currPage = request.getParameter("currentPage");

            if (currPage == null || "".equals(currPage.trim())) {
                currPage = "1";
            }

            int currentPage = Integer.parseInt(currPage);

            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(currentPage);


            InformationDAO dao = new InformationDAO();
            Map<String, Object> result = dao.getAll(pageBean);


            request.setAttribute("result", result);


            this.uri = "/sys/information/information.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int informationId = Integer.parseInt(request.getParameter("id"));

            Information information = new Information();
            information.setId(informationId);


            InformationDAO dao = new InformationDAO();
            dao.delete(information);


            this.uri = "/InformationServlet?method=list";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri)
                .forward((ServletRequest) request, (ServletResponse) response);
    }


    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String currPage = request.getParameter("currentPage");

            String InformationTitle = request.getParameter("title");

            if (currPage == null || "".equals(currPage.trim())) {
                currPage = "1";
            }

            int currentPage = Integer.parseInt(currPage);


            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(currentPage);

            Information title = new Information();
            title.setTitle(InformationTitle);


            InformationDAO dao = new InformationDAO();
            Map<String, Object> result = dao.search(pageBean, title);


            request.setAttribute("result", result);

            this.uri = "/sys/information/information.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri)
                .forward((ServletRequest) request, (ServletResponse) response);
    }


    public void Jumpup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int informationId = Integer.parseInt(request.getParameter("id"));

            Information information = new Information();
            information.setId(informationId);


            InformationDAO dao = new InformationDAO();
            Information result = dao.getone(information);


            request.setAttribute("listClass", result);

            this.uri = "/sys/information/update.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri)
                .forward((ServletRequest) request, (ServletResponse) response);
    }


    public void update(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            int informationId = Integer.parseInt(request.getParameter("id"));
            String informationTitle = request.getParameter("title");
            String informationAuthor = request.getParameter("author");
            String informationContent = request.getParameter("content");
            String informationDate = request.getParameter("date");


            Information information = new Information();
            information.setId(informationId);
            information.setTitle(informationTitle);
            information.setAuthor(informationAuthor);
            information.setContent(informationContent);
            information.setDate(informationDate);


            InformationDAO dao = new InformationDAO();
            dao.update(information);


            this.uri = "/InformationServlet?method=list";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri)
                .forward((ServletRequest) request, (ServletResponse) response);
    }

    public void init() throws ServletException {
    }
}


